package com.cmii.sjw.department.control.jsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmii.sjw.department.control.jsc.mapper.DistrictDetailMapper;
import com.cmii.sjw.department.control.jsc.model.DistrictDetail;
import com.cmii.sjw.department.control.jsc.service.DistrictDetailService;
import com.cmii.sjw.department.control.jsc.vo.DistrictDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Slf4j
@Service("DistrictDetailService")
public class DistrictDetailServiceImpl extends ServiceImpl<DistrictDetailMapper, DistrictDetail> implements DistrictDetailService {

    @Autowired
    private DistrictDetailMapper districtDetailMapper;


    /**
     * 区县详情
     *
     * @return 列表
     */
    @Override
    public IPage<DistrictDetail> list(Integer orderNum, Integer isDeleted, Integer current, Integer size) {
        IPage<DistrictDetail> page = new Page<>(current, size);
        return page(page, new LambdaQueryWrapper<DistrictDetail>()
                .eq(Objects.nonNull(orderNum), DistrictDetail::getOrderNum, orderNum)
                .eq(Objects.nonNull(isDeleted), DistrictDetail::getIsDeleted, isDeleted)
        );
    }

    /**
     * 区县详情
     *
     * @return 根据regional_level查出名称
     */
    @Override
    public List<DistrictDetailVO> listByRegionalLevel() {
        List<DistrictDetail> districtDetails = this.list(new LambdaQueryWrapper<DistrictDetail>()
                .eq(DistrictDetail::getRegionalLevel, "区县")
                .orderByAsc(DistrictDetail::getZoningCode)
                .select(DistrictDetail::getZoningName, DistrictDetail::getZoningCode));

        return districtDetails.stream()
                .map(d -> new DistrictDetailVO()
                        .setZoningCode(d.getZoningCode())
                        .setZoningName(d.getZoningName()))
                .collect(Collectors.toList());
    }
}
